<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button
            type="primary"
            size="small"
            @click="handleAddAccess(1, '0')"
            >添加权限</el-button
          >
        </div>
        <el-table :data="list" row-key="id" border>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                v-if="row.type === 1"
                size="small"
                type="text"
                @click="handleAddAccess(2, row.id)"
                >添加权限点</el-button
              >
              <el-button size="small" type="text" @click="handleEdit(row.id)"
                >查看</el-button
              >
              <el-button size="small" type="text" @click="handleDel(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 新增/编辑的弹层 -->
      <el-dialog
        :visible="showDialog"
        :title="dialogTitle"
        :close-on-click-modal="false"
        @close="handleCloseDialog"
      >
        <!-- 表单内容 -->
        <el-form label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right">
            <el-button @click="handleCloseDialog">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  reqGetPermissionList,
  reqAddPermission,
  reqDelPermission,
  reqGetPermissionDetail,
  reqUpdatePermission,
} from "@/api/permission";
import { TreeToList } from "@/utils/index";
export default {
  name: "Permissions",
  components: {},
  computed: {
    dialogTitle() {
      return this.formData.id ? "编辑权限" : "添加权限";
    },
  },
  created() {
    this.getPermissionList();
  },
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: "0", // 启用禁用, 0禁用, 1启用
        name: "", // 权限名称
        code: "", // 权限标识,用于将来判断
        description: "", // 权限描述
        type: "", // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: "", // 添加到哪个节点下
      },
    };
  },
  methods: {
    async handleEdit(id) {
      console.log(id);
      this.showDialog = true;
      const { data } = await reqGetPermissionDetail(id);
      this.formData = data;
    },
    handleDel(id) {
      this.$confirm("该操作不可逆,确定删除嘛?", "温馨提示")
        .then(async () => {
          const res = await reqDelPermission(id);
          console.log(res);
          this.$message.success("删除权限成功");
          this.getPermissionList();
        })
        .catch(() => {
          console.log("取消");
        });
    },
    handleCloseDialog() {
      this.showDialog = false;
      this.formData = {
        enVisible: "0",
        name: "",
        code: "",
        description: "",
        type: "",
        pid: "",
      };
    },
    async clickSubmit() {
      if (this.formData.id) {
        //   编辑权限
        const res = await reqUpdatePermission(this.formData)
        console.log(res);
        this.$message.success("编辑权限成功");
      } else {
        //   添加权限
        const res = await reqAddPermission(this.formData);
        console.log(res);
        this.$message.success("添加权限成功");
      }

      this.handleCloseDialog();
      this.getPermissionList();
    },
    handleAddAccess(type, pid) {
      this.showDialog = true;
      console.log(type, pid);
      this.formData.type = type;
      this.formData.pid = pid;
    },
    async getPermissionList() {
      const { data } = await reqGetPermissionList();
      console.log(data);
      this.list = TreeToList(data, "0");
      console.log(this.list);
    },
  },
};
</script>

<style lang="scss" scoped>
</style>